var AdsModule = function() {
	var editable = function() {
		
		return {
			init: function() {
				var oTable = $('#modelEditable').dataTable({
					"aLengthMenu": [
                        [5, 10, 20, 50],
                        [5, 10, 20, 50]
                    ],
//                    "autoWidth":false,
                    "scrollX": true,
					"displayLength": 10,
					"language": {
						"lengthMenu": "每页 _MENU_ 条记录",
						"paginate": {
							"previous": "上一页",
							"next": "下一页"
						},
						"info": "从 _START_ 到  _END_ 条，总记录为 _TOTAL_ 条",
						"infoEmpty": "记录数为0",
					},
					"columnDefs": [
					{
						"data": null,
						'sortable': false,
						"defaultContent": "<center>-</center>",
						"targets": [1]
					},{
						"data": null,
						'sortable': false,
						"defaultContent": "<center>-</center>",
						"targets": [2]
					},{
						"data": null,
						'sortable': false,
						"defaultContent": "<center>-</center>",
						"targets": [3]
					},{
						"data": null,
						'sortable': false,
						"defaultContent": "<center>-</center>",
						"targets": [4]
					},{
						"data": null,
						'sortable': false,
						"defaultContent": "<center>-</center>",
						"targets": [5]
					},{
						"data": null,
						'sortable': false,
						"defaultContent": "Edit",
						"targets": [6]
					}],
					"columns": [{
						"data": "thumbnail"
					}, {
						"data": "adsName"
					},{
						"data": "publishTime"
					},{
						"data": "orders"
					},{
						"data": "platform"
					},{
						"data": "state"
					}],
					"filter": false,
					"processing": false,
					"serverSide": true,
					"ajaxSource": ctx + "/noads/query",
					"serverMethod": "post",
					"fnRowCallback": function(nRow, aData,displayIndex) {
						nRow.children[0].id = aData.id;
						if(aData.thumbnail!=null){
							$('td:eq(0)', nRow).html('<img src="'+imagePrefix+aData.thumbnail+ '" width="128">');
						}
						if (aData.state == true) {
							$('td:eq(4)', nRow).html("已发布");
						} else {
							$('td:eq(4)', nRow).html("未发布");
						}
						if (aData.platform == 1) {
							$('td:eq(5)', nRow).html("所有平台");
						} else if(aData.platform == 2){
							$('td:eq(5)', nRow).html("投后APP");
						} else if(aData.platform == 3){
							$('td:eq(5)', nRow).html("理财师pad");
						} else {
							$('td:eq(5)', nRow).html("<center>-</center>");
						}
						$('td:eq(6)', nRow).html('<a class="edit fa fa-pencil-square-o fa-lg" title="编辑" href="javascript:;"></a> &nbsp;<a title="删除" class="delete fa fa-trash-o fa-lg" href="javascript:;"></a>');
						return nRow;
					},
					"fnServerData": function(ajaxSource, dataSet, fnCallback) {
						var json = "";
						var s;
						for (var i = 0; i < dataSet.length; i++) {
							s = dataSet[i];
							if (s.name == 'sEcho') {
								json = json + '"echo":' + s.value;
							} else if (s.name == 'iDisplayStart') {
								json = json + ',"start":' + s.value;
							} else if (s.name == 'iDisplayLength') {
								json = json + ',"pageSize":' + s.value;
							}
						}
						var form = $("form#queryForm").serializeJSON();
						var js = form.replace('}', ',');
						json = js + json + "}";
						$.ajax({
							"contentType": "application/json; charset=UTF-8",
							"dataType": 'json',
							"type": "POST",
							"url": ajaxSource,
							"data": json,
							"success": fnCallback
						});
					},
				});
				// 删除
				$('#modelEditable').on("click", "a.delete", function(e) {
					e.preventDefault();
					var obj = $(this);
					bootbox.confirm("您确定删除这条记录?", function(result) {
						if (result) {
							var nRow = obj.parents('tr')[0];
							var id = nRow.children[0].id;
							$.ajax({
								type: "post",
								url: ctx + "/noads/delete",
								data: "id=" + id,
								dataType: "json",
								success: function(msg) {
									if (msg.code == 1) {
										bootbox.alert("删除广告成功！");
										oTable.fnDeleteRow(nRow);
									} else {
										bootbox.alert("删除广告失败！");
									}
								},
								error: function(msg) {
									bootbox.alert(msg.message);
								}
							});
						}
					});
				});
				// 编辑
				$('#modelEditable').on("click", "a.edit", (function(e) {
					e.preventDefault();

					var nRow = $(this).parents('tr')[0];
					var id = nRow.children[0].id;
					window.location.href = ctx + "/noads/edit/" + id;
					
				}));
				// 明细
				$('#modelEditable').on("click", "a.detail", (function(e) {
					e.preventDefault();

					var nRow = $(this).parents('tr')[0];
					var id = nRow.children[0].id;

				}));

				$('#queryBtn').click(function() {
					oTable.fnClearTable();
				});
			}
		};
	}();

	return {
		init: function() {
			editable.init();
		}
	};
}();